import { CellType } from '@/idoo-core/table/add-row-table/IdooAddRowTable'

/**
 * 可视化页面配置文件
 * 包含表格列配置、数据配置等
 */

/**
 * 待排程工单表格列配置
 */
export const unselectedOrderColumns = [
  {
    prop: 'orderCode',
    label: '订单编号',
    type: CellType.INPUT,
    width: 280,
    required: true,
    placeholder: '请输入订单编号'
  },
  {
    prop: 'productCode',
    label: '产品编号',
    type: CellType.SELECT,
    width: 280,
    required: true,
    placeholder: '请选择产品编号',
    options: [] // 将在运行时动态加载产品数据
  },
  {
    prop: 'deliveryDate',
    label: '交付日',
    type: CellType.DATE_PICKER,
    width: 180,
    required: true,
    format: 'YYYY-MM-DD',
    valueFormat: 'YYYY-MM-DD'
  },
  {
    prop: 'quantity',
    label: '数量',
    type: CellType.INPUT_NUMBER,
    width: 180,
    required: true,
    min: 1,
    placeholder: '请输入数量'
  },
  {
    prop: 'priority',
    label: '工单优先级',
    type: CellType.SELECT,
    required: true,
    placeholder: '请选择优先级',
    options: [
      { label: '高', value: '高' },
      { label: '中', value: '中' },
      { label: '低', value: '低' }
    ]
  }
]

/**
 * 工单优先级选项
 */
export const priorityOptions = [
  { label: '高', value: '高' },
  { label: '中', value: '中' },
  { label: '低', value: '低' }
]

/**
 * 工单导入配置
 */
export const workOrderImportConfig = {
  entityName: 'model-excel-import-dto-20250902',
  templateName: '模板-'
}

/**
 * 基础参数初始数据
 */
export const baseParamsInitialData = {
  startDate: (() => {
    // 默认开始日期为今天 8点
    const today = new Date()
    today.setHours(8, 0, 0, 0) // 设置为上午8点
    
    // 格式化为 YYYY-MM-DD HH:mm
    const year = today.getFullYear()
    const month = String(today.getMonth() + 1).padStart(2, '0')
    const day = String(today.getDate()).padStart(2, '0')
    const hours = String(today.getHours()).padStart(2, '0')
    const minutes = String(today.getMinutes()).padStart(2, '0')
    console.log(`${year}-${month}-${day} ${hours}:${minutes}`)
    return `${year}-${month}-${day} ${hours}:${minutes}`
  })(),
  workOverTime: false,
  enableChangeLaneRate: false,
  changeLaneRate: 0,
  enableDeliveryRate: true, // 默认启用交期权重
  deliveryRate: 70.0, // 默认交期权重70%
  enableEquiptUseProbabilityRate: true, // 默认启用设备利用率权重
  equiptUseProbabilityRate: 30.0 // 默认设备利用率权重30%
}

/**
 * 工作时间初始数据
 */
export const workTimeInitialData = [
  {
    day: 1,
    dayName: '星期一',
    expanded: false,
    workPeriods: [
      { startTime: '08:00', endTime: '12:00', error: '' },
      { startTime: '13:00', endTime: '17:00', error: '' }
    ],
    overtimePeriod: null
  },
  {
    day: 2,
    dayName: '星期二',
    expanded: false,
    workPeriods: [
      { startTime: '08:00', endTime: '12:00', error: '' },
      { startTime: '13:00', endTime: '17:00', error: '' }
    ],
    overtimePeriod: null
  },
  {
    day: 3,
    dayName: '星期三',
    expanded: false,
    workPeriods: [
      { startTime: '08:00', endTime: '12:00', error: '' },
      { startTime: '13:00', endTime: '17:00', error: '' }
    ],
    overtimePeriod: null
  },
  {
    day: 4,
    dayName: '星期四',
    expanded: false,
    workPeriods: [
      { startTime: '08:00', endTime: '12:00', error: '' },
      { startTime: '13:00', endTime: '17:00', error: '' }
    ],
    overtimePeriod: null
  },
  {
    day: 5,
    dayName: '星期五',
    expanded: false,
    workPeriods: [
      { startTime: '08:00', endTime: '12:00', error: '' },
      { startTime: '13:00', endTime: '17:00', error: '' }
    ],
    overtimePeriod: null
  },
  {
    day: 6,
    dayName: '星期六',
    expanded: false,
    workPeriods: [],
    overtimePeriod: null
  },
  {
    day: 7,
    dayName: '星期日',
    expanded: false,
    workPeriods: [],
    overtimePeriod: null
  }
]

/**
 * 产品选项数据（运行时动态加载）
 */
export const productOptions = []

/**
 * 设备配置初始数据 - 默认为空，用户可以选择设备
 */
export const deviceInitialData = []

/**
 * 创建初始化配置数据的函数
 */
export const createInitialConfigData = () => {
  return {
    baseParameters: {
      ...baseParamsInitialData,
      constraintInfo: null // 约束信息将在运行时设置
    },
    workTimeConfiguration: [...workTimeInitialData],
    deviceConfiguration: [...deviceInitialData]
  }
}

/**
 * 全屏加载配置
 */
export const loadingConfig = {
  totalDelay: 100000,
  typingSpeed: 200
}

/**
 * 侧边栏配置
 */
export const asideConfig = {
  defaultWidth: 320,
  minWidth: 150
}


/**
 * 面板类型枚举
 */
export const PanelType = {
  ORDER_SELECTION: 'orderSelection',
  CONSTRAINTS: 'constraints'
}

/**
 * 错误提示消息
 */
export const ErrorMessages = {
  SAVE_EDITING_ROW: '请先保存正在编辑的工单',
  SELECT_ORDER_FIRST: '请先选择工单',
  SELECT_CONSTRAINT_FIRST: '请先选择约束配置',
  SAVE_ORDER_FAILED: '保存订单失败，请重试',
  LOAD_ORDERS_FAILED: '加载工单列表失败',
  LOAD_PRODUCTS_FAILED: '加载产品数据失败',
  IMPORT_FAILED: '工单导入失败，请检查数据格式',
  REFRESH_FAILED: '刷新数据失败',
  COMPLETE_CONFIG: '请完善配置信息：',
  LOAD_CONFIG_FAILED: '配置数据加载失败'
}

/**
 * 成功提示消息
 */
export const SuccessMessages = {
  ORDERS_SELECTED: (count) => `成功选择 ${count} 条订单`,
  IMPORT_SUCCESS: (count) => `成功导入${count}条工单数据`,
  CONFIG_LOADED: '配置数据加载成功',
  FORMS_RESET: '所有表单已重置',
  ROW_SAVED: '工单数据暂存成功',
  PRODUCTS_LOADED: (count) => `加载产品数据成功，共 ${count} 个产品`
}

/**
 * 加载提示消息
 */
export const LoadingMessages = {
  LOADING_ORDERS: '正在加载已选择工单...',
  SAVING_ORDERS: '正在保存选中的订单...',
  GENERATING_PLAN: (title) => `正在生成${title} -> 排程方案，请稍候...`
}

/**
 * 更新产品选项
 * @param {Array} products 产品列表
 */
export const updateProductOptions = (products) => {
  const productColumn = unselectedOrderColumns.find(col => col.prop === 'productCode')
  if (productColumn) {
    productColumn.options = products.map(product => ({
      label: `${product.productCode} - ${product.productName || ''}`,
      value: product.productCode
    }))
  }
}

export default {
  unselectedOrderColumns,
  priorityOptions,
  workOrderImportConfig,
  baseParamsInitialData,
  workTimeInitialData,
  deviceInitialData,
  createInitialConfigData,
  loadingConfig,
  asideConfig,
  PanelType,
  ErrorMessages,
  SuccessMessages,
  LoadingMessages,
  productOptions,
  updateProductOptions
}

